package com.fzebu.fastboot.workflow;

import com.engine.govern.constant.RemindType;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
import weaver.hrm.User;
import weaver.soa.workflow.request.*;

import java.util.List;
import java.util.Map;

/**
 * @author Y-Aron
 * @version 1.0.0
 * @create 2019/10/24 12:11
 */
public class WorkflowBuilder {

    @Getter
    private RequestInfo request;

    public WorkflowBuilder() {
        this.request = new RequestInfo();
        this.level(LevelEnum.DEFAULT);
        this.remindType(RemindType.REMINDTYPE_ISSUE);
    }

    public WorkflowBuilder title(String title) {
        this.request.setDescription(title);
        return this;
    }

    public WorkflowBuilder workflowId(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new IllegalArgumentException("workflow id is null");
        }
        this.request.setWorkflowid(id);
        return this;
    }

    public WorkflowBuilder userId(Integer userId) {
        User user = User.getUser(userId, 0);
        if (user == null) {
            throw new IllegalArgumentException("creator is null");
        }
        this.request.setCreatorid(userId + "");
        return this;
    }

    public WorkflowBuilder level(LevelEnum level) {
        this.request.setRequestlevel(level.getValue());
        return this;
    }

    public WorkflowBuilder remindType(RemindType remindType) {
        this.request.setRemindtype(remindType.getCode());
        return this;
    }

    public WorkflowBuilder request(RequestInfo request) {
        this.request = request;
        return this;
    }

    public WorkflowBuilder mainTable(Map<String, Object> mainTable) {
        if (mainTable == null || mainTable.size() == 0) {
            return null;
        }
        MainTableInfo mt = new MainTableInfo();
        mainTable.forEach((k, v) -> mt.addProperty(new Property(){{
            setName(k);
            setValue(String.valueOf(v));
        }}));
        this.request.setMainTableInfo(mt);
        return this;
    }

    public WorkflowBuilder addDetailTable(List<Map<String, Object>> mapList) {
        return this.addDetailTable(-1, mapList);
    }

    public WorkflowBuilder addDetailTable(int detailId, List<Map<String, Object>> mapList) {
        if (mapList == null) {
            return this;
        }
        DetailTableInfo dti = this.request.getDetailTableInfo();
        if (dti == null) {
            dti = new DetailTableInfo();
        }
        DetailTable dt = new DetailTable();
        Row row;
        for (Map<String, Object> map : mapList) {
            row = new Row();
            Cell cell;
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                cell = new Cell();
                cell.setName(entry.getKey());
                cell.setValue(String.valueOf(entry.getValue()));
                row.addCell(cell);
            }
        }
        if (detailId >= 0) {
            dt.setId(String.valueOf(detailId));
            dti.addDetailTable(detailId, dt);
        } else {
            dti.addDetailTable(dt);
        }
        return this;
    }

    public WorkflowBuilder isNextFlow(boolean isNextFlow) {
        if (!isNextFlow) {
            this.request.setIsNextFlow("0");
        }
        return this;
    }
}
